package com.sun.mvc.controller;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sun.mvc.model.Employee;
import com.sun.mvc.service.EmployeeService;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
	@Autowired
	EmployeeService service;

	/**
	 * http://localhost/springmvc/employee/list
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getAllEmployees(Model model) {
		model.addAttribute("employees", service.getEmployees());
		return "/employee/employeeList";
	}

	/**
	 * http://localhost/springmvc/employee/get?username=NAME3
	 * 
	 * @param userName
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public ModelAndView getEmployee(@RequestParam String username) {
		ModelAndView view = new ModelAndView("/employee/employeeView");
		view.addObject("employee", service.getEmployeeByName(username));
		return view;
	}

	/**
	 * http://localhost/springmvc/employee/get/NAME2
	 * 
	 * @param username
	 * @return
	 */
	@RequestMapping(value = "/get/{username}", method = RequestMethod.GET)
	public ModelAndView get(@PathVariable(value = "username") String username) {
		ModelAndView view = new ModelAndView("/employee/employeeView");
		view.addObject("employee", service.getEmployeeByName(username));
		return view;
	}

	/**
	 * http://localhost/springmvc/employee/add?id=12312312&name=
	 * Name11&age=11
	 * http://localhost/springmvc/employee/add?id=12312312&name=
	 * Name11&age=11&name=12
	 * 
	 * @param employee
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	// public void add(@ModelAttribute("employee") Employee employee) {
	public ModelAndView add(Employee employee) {
		System.out.println(employee);
		ModelAndView view = new ModelAndView("/employee/employeeView");
		view.addObject("employee", employee);
		return view;
	}
	
	/**
	 * http://localhost/springmvc/employee/addByEntity?entity.id=1&entity.name=A&entity.name=B
	 * @param entity
	 * @return
	 */
//	@RequestMapping(value = "/addEntity", method = RequestMethod.GET)
//	public ModelAndView addByEntity(@ModelAttribute("entity") Employee employee) {
//		System.out.println(employee);
//		ModelAndView view = new ModelAndView("/employee/employeeView");
//		view.addObject("employee", employee);
//		return view;
//	}

	/**
	 * http://localhost/springmvc/employee/converterBase?employee=
	 * 123-22-31
	 * 
	 * @param employee
	 * @return
	 */
	@RequestMapping(value = "/converterBase", method = RequestMethod.GET)
	public ModelAndView converterBase(Employee employee) {
		ModelAndView view = new ModelAndView("/employee/employeeView");
		view.addObject("employee", employee);
		return view;
	}

	@RequestMapping(value = "/upload")
	public ModelAndView upload(Employee employee) {
		ModelAndView view = new ModelAndView("/employee/employeeView");
		view.addObject("employee", employee);
		return view;
	}

	/**
	 * @param response
	 * @param fileId
	 */
	@RequestMapping(value = "/download")
	public void download(HttpServletResponse response, String fileId) {
		byte[] bytes = service.getFileByte(fileId);
		response.reset();
		try {
			response.setContentType("application/octet-stream");
			response.addHeader("Content-disposition", "attachment;filename=" + new String("aaa.docx".getBytes("GBK"), "ISO8859_1"));
			response.addHeader("Content-length", String.valueOf(bytes.length));
			response.setStatus(200);
			
			OutputStream out = new BufferedOutputStream(response.getOutputStream());
			out.write(bytes);
			out.flush();
			out.close();
		} catch (Exception e) {
		}
	}

	/**
	 * http://localhost/springmvc/employee/toupload
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toupload", method = RequestMethod.GET)
	public ModelAndView toUpload() {
		return new ModelAndView("/upload/employeeAdd");
	}
	
	/**
	 * http://localhost/springmvc/employee/get.json/NAME1
	 * @param username
	 * @return
	 */
	@RequestMapping(value = "/get.json/{username}", method = RequestMethod.GET)
	@ResponseBody
	public Employee getEmp(@PathVariable String username) {
		Employee emp = service.getEmployeeByName(username);
		return emp;
	}
	
	/**
	 * http://localhost/springmvc/employee/addarray?name=www&id=11&name=qqq&age=2
	 * @param username
	 * @return
	 */
	@RequestMapping(value = "/addarray")
	public ModelAndView addArray(Employee employee) {
		ModelAndView view = new ModelAndView("/employee/employeeView");
		view.addObject("employee", employee);
		return view;
	}
}
